
var msico = "ydico";
var kaiID ;
var nkaiID  ;
var adtypes="";
var diinfo;
var temobj;
var kailen;
var ndiinfo;
var huafei;
var zmei=0;
var Qfmin= new Array();
	Qfmin[0] = 300; Qfmin[1] = 3000;
var Qfmax= new Array();
	Qfmax[0] = 3000; Qfmax[1] = 30000;



$(document).ready(function() {

	window.onload = function() { $(".gaidiv").css({display:"none"});	$("#load").css({display:"none"});	}; 
	$(".diinfo").fadeTo("fast",0.0);
	if($("#huafei").html()!="0"){
		 $("#hfico").html("<img src='/"+imgsrc+"/hfico.gif' onclick=\"mico('hfico')\" />") ;
	}
	$(".di").hover( function(){ $(this).find(".diinfo").fadeTo("fast",0.8);  },
				    function(){ $(this).find(".diinfo").fadeTo("fast",0.0);  }				   
	);


/*---------扩展或者增加金豆----------*/
	$(".di").bind("click",function(){
		$(this).unbind("click");
		if(msico=="kaiico" && $(this).html().indexOf("diinfo")==-1){
			adtypes = "new"; //第一次种植		
			kaiID = $(this).attr("id");
			kaiID = kaiID.replace("di","");  //土地的ID
			money_now = $("#money_now").val();  //金钱
			if(kaiID<11){  //普通土地
				if(parseInt(money_now) < 300){
					alert("您没有这么多花了！快去购买花吧~~！");
				}else{
					txt = 300;
				}
			}
			else{
				if(parseInt(money_now) < 900){
					alert("您没有这么多花了！快去购买花吧~~！");
				}else{
					txt = 900;
				}
			}
			tishi2();
			$.post("/Index/add_renshen",{addou:txt,kid:kaiID,adtype:adtypes},function(result){
				tishi2close();
				$(".gaidiv").css({display:"none"}); 
				$("#aform").css({display:"none"}); 
				$("#dousum").val("");
				if ( result.indexOf("ok")>0){
					$("#di"+kaiID).html("<img src='/"+imgsrc+"/shu2"+""+imgt+"' /><div class='diinfo'>"+result.split("-")[1]+"颗</div>");
					if(kaiID>=11){	
						$("#di"+kaiID).css({background:"url(/"+imgsrc+"/dibg11"+imgt+") top left no-repeat"}); }
					else{	
						$("#di"+kaiID).css({background:"url(/"+imgsrc+"/dibg01"+imgt+") top left no-repeat"}); 
					}
					
					$(".diinfo").fadeTo("fast",0.0);				
					msico = "";
					$("#body").css({cursor:""}); 
					if(adtypes=="ad"){
						zmei = ($("#zmei").html()*1)+(txt*1);
					}
					else{
						zmei = ($("#zmei").html()*1.00)+(result.split("-")[1]*1.00);  //播种
					}
					$("#zmei").html(zmei);
					
					stock = $("#stock").html()*1.00-result.split("-")[1]*1.00;
					//处理仓库数据
					if(stock >= 0){
						$("#stock").html(stock);

						zong =$("#zong").html()*1.00+result.split("-")[1]*1.00;
						$("#zong").html(zong);

					}

					
				}else{
					layer.msg(result);
				}
				
			} ); 
		}

		//增加播种事件
		if(msico=="jiaico" && $(this).html().indexOf("diinfo")>-1){
			adtypes = "ad";
			$(".gaidiv").fadeTo("fast",0.3);
			$("#adbtn").html("增加播种");
			$("#aform").fadeTo("fast",1);
			kaiID = $(this).attr("id");
			kaiID = kaiID.replace("di","");
			var money_now = $(this).find('.diinfo').html();
			if(kaiID<11){
				if(money_now < 2000){
					$("#max_add").html(2000-money_now);
				}
				else{
					$("#max_add").html(0);
				}
			}
			else{
				if(money_now < 20000){
					$("#max_add").html(20000-money_now);
				}
				else{
					$("#max_add").html(0);
				}
			}
		}  
		//施肥动作
		if(msico=="hfico" && $(this).html().indexOf("diinfo")>-1){
			kaiID = $(this).attr("id");
			kaiID = kaiID.replace("di","");
			$("#shifei").fadeTo(100,1);	
			$("#shifei img").animate({height:"159px",width:"358px",marginTop:"80px"},400,function(){$("#shifei img").animate({marginTop:"70px"},200);});
			$("#shifei").fadeTo(1000,0.0,function(){
				$("#shifei").css({display:"none"}); 
				$.post("/Index/shifei",{tiandi_type:2},function(result){
					if (result.indexOf("ok")>0){
						/*huafei = result.split("-")[1] ;
						$("#fei").html(huafei);						
						$("#di"+kaiID).find("img").attr("src",imgsrc+"/shu"+getshu(result.split("-")[2],kaiID)+imgt);
						$("#di"+kaiID).find(".diinfo").html(result.split("-")[2]);
						//alert( ( result.split("-")[3]).toFixed(2));
						$("#di"+kaiID).find(".zeng").html("+"+result.split("-")[3]);
						temobj = $("#di"+kaiID).find(".zeng");						
						temobj.fadeTo(100,0.9);
						temobj.animate({top:"0px"},400);
						temobj.fadeTo(1100,0.0)	;
						$("#zmei").html((result.split("-")[3]*1)+($("#zmei").html()*1));
						
						zong =($("#zmei").html()*1)+($("#stock").html()*1);
						$("#zong").html(zong);

						growth_total = (result.split("-")[3]*1)+($("#growth_total").html()*1);
						$("#growth_total").html(growth_total);

						$("#huafei").html(huafei);
						if(huafei==0){
							$("#body").css({cursor:""});  
							msico="";	
							$("#hfico").html("<img src='/"+imgsrc+"/hfico2"+imgt+"' />") ;	
						}*/
						window.location.href= location.href;
					}
					else alert(result.msg);
				} );					
			});
		}

		//收获动作
		if(msico=="caiico" && $(this).html().indexOf("diinfo")>0){
			kaiID = $(this).attr("id");
			kaiID = kaiID.replace("di","");
			$("#caiico").html("<img src='/"+imgsrc+"/caiico2"+imgt+"' />") ;	
			
			if(myself){
				$.post("/Index/shouhuo",{farmID:kaiID,tiandi:2},function(result){
					if (result.indexOf("ok")>0){
						layer.msg('收割成功!');
						
						window.location.href = window.location.href;

						

					}
					else alert(result);
				} );
			}
		}

		//收获动作
		if(msico=="tou" && $(this).html().indexOf("diinfo")>0){
			tishi2();
			$.post("/Friend/tou",{tiandi:2,user:user,layer:layers},function(result){
				tishi2close();
				if (result.indexOf("ok")>0){
					/*if(kaiID<=10){ 
						$("#di"+kaiID).find("img").attr("src","/"+imgsrc+"/shu2"+imgt+"");
						$("#di"+kaiID).find(".diinfo").html(Qfmin[0]);
					}
					else{
						$("#di"+kaiID).find("img").attr("src","/"+imgsrc+"/shu2"+imgt+"");
						$("#di"+kaiID).find(".diinfo").html(Qfmin[1]);

					}*/
					$("#zmei").html(($("#zmei").html()*1)-(result.split("-")[1]*1));
				
					
					stock = ($("#stock").html()*1)+(result.split("-")[1]*1);
					if(stock > 0){
						$("#stock").html(stock);
					}
					

					$("#body").css({cursor:""}); 
					msico="";

					

				}else{
					layer.msg(result);
				} 
			});
		}


		
		/*向好友采蜜*/
		if(msico=="caimi" && is_caimi && !myself){
			tishi2();						
			$.post("/Friend/caimi",{username:user,tiandi_type:tiandi_type},function(data){
				tishi2close();			
				if(data.status == 1){
					layer.msg(data.msg);
					$("#caimi").html('<img src="/imsges/mf2.png"  alt="采蜜" />') ;
					window.location.href= location.href;					
				}else{
					layer.msg(data.msg);
				}				  	
			},'json');
			return false;
		}

		/*一键采蜜*/
		if(msico=="caimi2" ){
			kaiID = $(this).attr("id");
			kaiID = kaiID.replace("di","");
			username = $("#unumber").text();
			username = username.replace("用户：","");
		
			$.post("caimi.php",{username:username,farmID:kaiID},function(result){
				   alert("采蜜的数量为"+result.split("-")[1]);
					
			} );
		}

	});
	
	
	$("#adbtn").click(function(){
		$("#adbtn").attr({"disabled":"disabled"});
		money_now = $("#money_now").val();
		txt=$("#dousum").val();
		if(txt <= 0 || txt -  money_now >0){
			layer.msg('请输入正确的数量');
			$("#adbtn").removeAttr('disabled');
			return;
		}
		$(".gaidiv").css({display:"none"}); 
		$("#aform").css({display:"none"}); 

		$.post("/Index/add_rs",{addou:txt,kid:kaiID,adtype:adtypes,now:money_now},function(data){
			
			$("#dousum").val("");
			if (data.status == 1){
				layer.msg('增加果子成功');
				$("#adbtn").removeAttr('disabled');
				window.location.href = window.location.href;
				//$("#jiaico").html("<img src='/"+imgsrc+"/jiaico2"+imgt+"' />") ;	
			}else{
				$("#adbtn").removeAttr('disabled');
				layer.msg(data.msg);
			}
		},'json');  
	});
	
	
	
	$("#closes").click(function(){
			$(".gaidiv").css({display:"none"}); 
			
			$("#aform").css({display:"none"}); 	});

	
}); 

/*	点击工具栏改变鼠标样式*/
function mico(ico){
	msico = ico;
	$("#body").css({cursor:"url('/"+imgsrc+"/"+msico+".ico'),auto "});   
	//alert(msico);
}

/*	点击工具栏改变鼠标样式*/
function getshu(dous,id){
	var tdou ;
	if(id<=10){
		tdou=dous/100;
	}else{
		tdou=dous/1000;
	} 
	if(id<=10)
	{
		if (tdou==3) return 1;
		if (tdou>3 && tdou<=12) return 1;
		if (tdou>12 && tdou<=21) return 1;
		if (tdou>21 && tdou<=30) return 1;
	}
	else
	{
		return 2;
		/*if (tdou==3) return 2;
		if (tdou>3 && tdou<=12) return 2;
		if (tdou>12 && tdou<=21) return 2;
		if (tdou>21 && tdou<=30) return 2;*/
	}
	
}
 //alert(getshu(1333));

 